﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Showroom.BLL;
using System.Data;

namespace MobileWeb
{
    public partial class Products : System.Web.UI.Page
    {
        protected int CategoryID
        {
            get
            {
                if (string.IsNullOrEmpty(Request["cat"]))
                {
                    return 0;
                }
                else
                {
                    return Convert.ToInt32(Request["cat"].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)[1]);
                }
            }
        }

        protected string CategoryName
        {
            get
            {
                if (string.IsNullOrEmpty(Request["cat"]))
                {
                    return string.Empty;
                }
                else
                {
                    return Request["cat"].Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)[0];
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                ((Site)Page.Master).SetTitlePage(CategoryName);

                //load list 5 products first
                View_ListProducts cProds = new View_ListProducts();
                string filter = string.Format("{0} = {1}", View_ListProducts.ColumnNames.CategoryID, CategoryID);
                cProds.Search(filter, View_ListProducts.ColumnNames.ID + " DESC", 0, 5);
                rptProductList.DataSource = cProds.DefaultView;
                rptProductList.DataBind();

                View_Category cat = new View_Category();
                cat.Where.ID.Value = CategoryID;
                cat.Query.Load();

                if (!string.IsNullOrEmpty(cat.s_ParentID))
                {
                    int parentID = cat.ParentID;
                    cat = new View_Category();
                    cat.Where.ID.Value = parentID;
                    cat.Query.Load();

                    ((Site)Page.Master).SetBackUrl(string.Format("Category.aspx?cat={0}_{1}", cat.Name, cat.ID));
                }
                else
                    ((Site)Page.Master).SetBackUrl("Default.aspx");
            }
        }
    }
}